//
//  TopImageTableViewCell.m
//  XIWenLeJian
//
//  Created by lanou3g on 15/11/18.
//  Copyright © 2015年 蓝足. All rights reserved.
//

#import "TopImageTableViewCell.h"
#import "NewsModel.h"
#import "UIImageView+WebCache.h"
#import "Masonry.h"

@interface TopImageTableViewCell ()

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIImageView *pngImageView;
@property (nonatomic,strong) UIImageView *mainImageView;

@end


@implementation TopImageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _mainImageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_mainImageView];
        
        _pngImageView = [[UIImageView alloc] init];
        _pngImageView.image = [UIImage imageNamed:@"night_photoset_list_cell_icon"];
        [self.contentView addSubview:_pngImageView];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_titleLabel];
    }
    return self;
}

- (void)setModel:(NewsModel *)model
{
    [self.mainImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-30);
    }];
    [self.pngImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(5);
        make.top.equalTo(self.mainImageView.mas_bottom).offset(5);
        make.size.mas_equalTo(CGSizeMake(22, 22));
    }];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.pngImageView.mas_right).offset(5);
        make.top.equalTo(self.pngImageView);
        make.bottom.mas_equalTo(-5);
        make.right.mas_equalTo(-5);
    }];
    if (_model != model) {
        if (model.headImage == nil) {
            [self.mainImageView setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:[UIImage imageNamed:@"picholder"]];
        }else
        {
            self.mainImageView.image = model.headImage;
        }
        self.titleLabel.text = model.title;
    }
}

+ (instancetype)addTopImageCellWithTableView:(UITableView *)tableView model:(NewsModel *)model
{
    NSString *cellIdentifier = @"TopImageCell";
    TopImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[TopImageTableViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:cellIdentifier];
    }
    cell.model = model;
    return cell;
    
}


@end
